<template>
  <el-dialog :visible.sync="examineShow" width="40%" @close='dialogClose'
  :close-on-click-modal='false'
  :close-on-press-escape='false'>
   <h2 v-if="title" slot="title" class="tx-c">{{title}}</h2>
   <slot></slot>
    <el-form autocomplete="off"  label-width="85px" :model="options" ref="form" v-if="!item.view">
      <el-form-item label="审核结果"  prop="review_status" class="tip" v-if="!comeBack">
        <el-radio-group v-model="options.review_status">
          <el-radio :label="1">通过</el-radio>
          <el-radio :label="2">不通过</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="comeBack?'退审原因':'审核意见'" v-show="options.review_status === 2" class="tip">
        <el-input
          size="medium"
          type="textarea"
          :autosize='{ minRows: 4, maxRows: 10 }'
          v-model.trim="options.review_comment"
          placeholder="请输入审核意见" />
      </el-form-item>
    </el-form>
    <el-form autocomplete="off"  label-width="90px" class="form" v-else>
      <el-form-item label="审核结果：">
        <p>{{ echoStatus(item.review_status)}}</p>
      </el-form-item>
      <el-form-item label="审核意见：">
        <p>{{item.review_comment || '无'}}</p>
      </el-form-item>
    </el-form>
    <div slot="footer" class="flex-justify-bcenter" v-if="!item.view">
      <el-button @click="examineShow = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
    name:'examine',
    props:{
      title:{
        default: '',
      },
      item:{
        default: ()=>({view:false})
      },
      comeBack:{  //退审功能，审核通过后使用  禁用通过
        default: false,
        type: Boolean,
      }
    },
    data(){
        return{
            examineShow: false,
            options:{
                review_status: null,
                review_comment: null
            },
            enterFn: () => {
              let theEvent = window.event || e;
              let code = theEvent.keyCode || theEvent.which || theEvent.charCode;
              if (code == 13 && this.examineShow && !this.item.view) {
                this.submit()
              }
            }
        }
    },
    mounted(){
      document.addEventListener('keydown',this.enterFn)
    },
    destroyed(){
      document.removeEventListener('keydown',this.enterFn)
    },
    methods:{
        echoStatus(i){
          const obj = {
            0: "未审核",
            1: "通过",
            2: "不通过",
          }
          return obj[i]
        },
        dialogClose(){
            this.options.review_status = null
            this.options.review_comment = null
        },  
        submit(){
            if(!this.options.review_status){
               this.$message.error('请先选择审核结果')
               return
            }
            if(this.options.review_status === 2 && !this.options.review_comment){
              this.$message.error('请输入审核意见')
              return
            }
            let options = {...this.options}
            if(options.review_status === 1) options.review_comment = ''
            options.reviewer_id = this.$store.state.userInfo.employees_id
            this.$emit('submit',options)
            this.examineShow = false
            
        }
    }
};
</script>
 
<style lang='less' scoped>

</style> 